package cn.tedu.gateway.constant;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/*
 * 本类用于定义需要放行的路径白名单
 * */
@Component
public class UriWhiteListConstants {

    /*
     * 登录请求路径
     * */
    String sso_uri = "http://localhost:9091/font/sso/**";
    String tag_uri = "http://localhost:9091/home/**";
    String register_uri = "http://localhost:9091/font/client/register";
    String file_upload_uri = "http://localhost:9091/file/upload";

    private String[] whitelistedPaths;

    // 构造函数中初始化白名单路径数组
    public UriWhiteListConstants() {
        initializeWhitelistedPaths();
    }

    // 初始化白名单路径数组的方法
    private void initializeWhitelistedPaths() {
        whitelistedPaths = new String[] {sso_uri,register_uri,file_upload_uri,tag_uri};
    }

    // 可选：提供一个方法来获取白名单路径列表
    public List<String> getWhitelistedPathsAsList() {
        return Arrays.asList(whitelistedPaths);
    }


    /**
     * 判断请求路径是否在白名单中。
     *
     * @param requestPath 请求路径
     * @return 是否在白名单中
     */
    public boolean isInWhitelist(String requestPath) {
        List<String> whitelistedPaths = getWhitelistedPathsAsList();
        for (String path : whitelistedPaths) {
            if (path.endsWith("**") && requestPath.startsWith(path.substring(0, path.length() - 2))) {
                return true;
            } else if (path.equals(requestPath)) {
                return true;
            }
        }
        return false;
    }

}
